<?php
declare (strict_types = 1);

namespace app\controller\backend\home;

use app\controller\backend\BackendController;
use app\facade\StringFacade;
use app\service\logic\common\home\StatisticsService;
use app\service\logic\common\housing\HousingService;
use app\service\logic\common\order\CashService;
use app\service\logic\common\order\ReturnService;
use app\service\logic\common\order\SalesService;
use Carbon\Carbon;
use think\App;
use think\response\Json;

class StatisticsController extends BackendController
{
    protected $service;

    public function __construct(App $app, StatisticsService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 各个总数汇总
     *
     * @return Json
     */
    public function total()
    {
        $startAt = Carbon::now()->startOfDay()->timestamp;
        $endAt = Carbon::now()->endOfDay()->timestamp;

        // 房源总量
        $housingTotal = invoke(HousingService::class)->getCount([]);
        // 今日房源在租数量(次数), 包括(房源销售订单 + 业主回家)
        $salesCount = invoke(SalesService::class)->getCountIncludeStartEnd($startAt, $endAt);
        $returnCount = invoke(ReturnService::class)->getCountIncludeStartEnd($startAt, $endAt);
        $rentingTotal = $salesCount + $returnCount; // 在租订单数量
        // 今日房源未租数量(次数)
        $salesTotal = invoke(SalesService::class)->getCount([], [], []);
        $returnTotal = invoke(ReturnService::class)->getCount([], [], []);
        $orderTotal = $salesTotal + $returnTotal;
        $noRentingTotal = $orderTotal - $rentingTotal;

        return amis([
            // 房源总量
            'housing_total' => (string)$housingTotal,
            // 房源在租订单数量
            'renting_total' => (string)$rentingTotal,
            // 房源未在租订单数量
            'not_renting_total' => (string)$noRentingTotal,
        ]);
    }

    /**
     * 业主分润
     *
     * @return Json
     */
    public function sharing()
    {
        // 房源收益总金额
        $housingIncome = invoke(SalesService::class)->sumRevenueBySearch(['type'],['type' => 5]);
        // 提现总额
        $cashTotal = invoke(CashService::class)->sumCashBySearch();
        // 剩余(待提现)
        $last = (float)bcsub((string)$housingIncome, (string)$cashTotal, 2);

        return amis([
            // 房源收益总金额
            'housing_income' => $housingIncome,
            // 提现总额
            'cash_total' => $cashTotal,
            // 剩余(待提现)
            'last' => $last <= 0 ? 0 : $last,
        ]);
    }

    /**
     * 年度租房金额统计
     *
     * @return Json
     */
    public function salesA()
    {
        set_time_limit(0);
        $params = $this->request->param();
        $search_fields = ['type', 'createtime'];
        $search = request()->only($search_fields);
        $search['type'] = 5;

        $createtimeRange = invoke(StatisticsService::class)->createtimeDefaultYear($params);
        $search['createtime'] = $createtimeRange['start'] . ',' . $createtimeRange['end'];
        $months = StringFacade::getMonthByCreatetime($search['createtime']);

        // 房源收益总金额
        $revenueTotal = invoke(StatisticsService::class)->getMonthRevenueTotal($params, $months, $search_fields, $search);

        return amis([
            'months' => $months,
            'revenue_total' => $revenueTotal,
        ]);
    }

    /**
     * 年度租房次数统计
     *
     * @return Json
     */
    public function salesC()
    {
        set_time_limit(0);
        $params = $this->request->param();
        $search_fields = ['createtime'];
        $search = request()->only($search_fields);

        $createtimeRange = invoke(StatisticsService::class)->createtimeDefaultYear($params);
        $search['createtime'] = $createtimeRange['start'] . ',' . $createtimeRange['end'];
        $months = StringFacade::getMonthByCreatetime($search['createtime']);

        // 房源订单总次数
        $timesTotal = invoke(StatisticsService::class)->getMonthTimesTotal($params, $months, $search_fields, $search);

        return amis([
            'months' => $months,
            'times_total' => $timesTotal,
        ]);
    }
}
